﻿using System;
using System.Net;
using System.Net.Mail;

public class EmailService
{
    /// <summary>
    /// Send an email using the specified parameters.
    /// </summary>
    /// <param name="fromAddress">Sender's email address</param>
    /// <param name="fromPassword">Sender's password</param>
    /// <param name="smtpHost">SMTP server address</param>
    /// <param name="smtpPort">SMTP server port</param>
    /// <param name="enableSsl">Use SSL or not</param>
    /// <param name="toAddress">Recipient's email address</param>
    /// <param name="subject">Email subject</param>
    /// <param name="body">Email body</param>
    /// <param name="isBodyHtml">Is the body HTML format</param>
    public static void SendEmail(
         string fromAddress,
         string fromPassword,
         string smtpHost,
         int smtpPort,
         bool enableSsl,
         string toAddress,
         string subject,
         string body,
         bool isBodyHtml = true,
         List<string> attachmentPaths = null)
    {
        // Create the email message
        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress(fromAddress);
        mailMessage.To.Add(toAddress);
        mailMessage.Subject = subject;
        mailMessage.Body = body;
        mailMessage.IsBodyHtml = isBodyHtml;

        // Add attachments if any
        if (attachmentPaths != null)
        {
            foreach (var attachmentPath in attachmentPaths)
            {
                // Read the file into a stream
                using (FileStream fileStream = new FileStream(attachmentPath, FileMode.Open, FileAccess.Read))
                {
                    Attachment attachment = new Attachment(fileStream, Path.GetFileName(attachmentPath));
                    mailMessage.Attachments.Add(attachment);
                }
            }
        }

        // Create the SMTP client
        SmtpClient smtpClient = new SmtpClient(smtpHost)
        {
            Port = smtpPort,
            EnableSsl = enableSsl,
            Credentials = new NetworkCredential(fromAddress, fromPassword)
        };

        try
        {
            // Send the email
            smtpClient.Send(mailMessage);
            Console.WriteLine("Email sent successfully!");
        }
        catch (Exception ex)
        {
            // Handle exceptions
            Console.WriteLine("Failed to send email: " + ex.Message);
        }
    }
}